package com.stu.manager.personnel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.stu.manager.config.Result;
import com.stu.manager.course.entity.SysCourse;
import com.stu.manager.course.mapper.SysCourseMapper;
import com.stu.manager.organization.entity.SysClassinfo;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.SysProfessional;
import com.stu.manager.organization.mapper.SysClassinfoMapper;
import com.stu.manager.organization.mapper.SysDepartmentMapper;
import com.stu.manager.organization.mapper.SysProfessionalMapper;
import com.stu.manager.personnel.entity.SysStudent;
import com.stu.manager.personnel.entity.SysTeacher;
import com.stu.manager.personnel.mapper.SysStudentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-15
 */
@Service
public class SysStudentServiceImpl extends ServiceImpl<SysStudentMapper, SysStudent> {

    @Resource
    private SysDepartmentMapper departmentMapper;

    @Resource
    private SysProfessionalMapper professionalMapper;

    @Resource
    private SysClassinfoMapper classinfoMapper;

    public IPage<SysStudent> pageStudent(Page<SysStudent> pageStudent, QueryWrapper<SysStudent> wrapper) {
        IPage<SysStudent> sysStudentIPage = baseMapper.selectPage(pageStudent, wrapper);
        return sysStudentIPage;
    }
    //添加学生
    public Result addStudent(SysStudent sysStudent) {
        Long idNumber = sysStudent.getIdNumber();
        QueryWrapper<SysStudent> wrapperIdNumber = new QueryWrapper<>();
        wrapperIdNumber.eq("id_number",idNumber);
        Integer count = baseMapper.selectCount(wrapperIdNumber);
        if(count>0){
            return Result.error().message("该学生已注册");
        }
        //先得到学院和专业的编码，入职时间，生成工号
        String departmentName = sysStudent.getDepartmentName();
        String professionalName = sysStudent.getProfessionalName();
        String classCode = sysStudent.getClassCode();
        if(StringUtils.isEmpty(departmentName) || StringUtils.isEmpty(professionalName) || StringUtils.isEmpty(classCode)){
            return Result.error().message("请选择学院|专业|班级！");
        }
        Integer hiredate = sysStudent.getHiredate();
        QueryWrapper<SysClassinfo> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("class_grade",hiredate).eq("class_code",classCode).eq("professional_name",professionalName);
        Integer count1 = classinfoMapper.selectCount(wrapper3);
        if(count1 == 0){
            return Result.error().message("当前年级未生成班级");
        }
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_name",departmentName);
        SysDepartment sysDepartment = departmentMapper.selectOne(wrapper);
        String departmentCode = sysDepartment.getDepartmentCode();
        QueryWrapper<SysProfessional> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("professional_name",professionalName);
        SysProfessional sysProfessional = professionalMapper.selectOne(wrapper1);
        String professionalCode = sysProfessional.getProfessionalCode();
        String subIdNum = sysStudent.getHiredate().toString() + departmentCode + professionalCode + sysStudent.getClassCode();
        QueryWrapper<SysStudent> wrapper2 = new QueryWrapper<>();
        wrapper2.likeRight("stu_number",subIdNum);
        List<SysStudent> sysStudents = baseMapper.selectList(wrapper2);
        if(sysStudents.isEmpty()){ //所添加老师同专业及入职时间无同事那么拼接4个0
            subIdNum =  subIdNum + "0000";
            long l = Long.parseLong(subIdNum);//拼接后工号
            sysStudent.setStuNumber(l);
        }else{
            Optional<Long> max = sysStudents.stream().map(SysStudent::getStuNumber).max(Long::compare);
            long l = max.get() + 1;
            sysStudent.setStuNumber(l);
        }
        int insert = baseMapper.insert(sysStudent);
        if (insert > 0) {
            return Result.ok().message("添加成功！");
        } else {
            return Result.error().message("添加失败！");
        }
    }

    //查询单个学生信息
    public SysStudent getStudentById(String id) {
        QueryWrapper<SysStudent> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        SysStudent sysStudent = baseMapper.selectOne(wrapper);
        return sysStudent;
    }

    //修改学生信息
    public Result updateStudent(SysStudent sysStudent) {
        //判断教师身份证是否已被注册
        Long idNumber = sysStudent.getIdNumber();
        QueryWrapper<SysStudent> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("id_number",idNumber).ne("id",sysStudent.getId());
        SysStudent sysStudent1 = baseMapper.selectOne(wrapper3);
        System.out.println(sysStudent1);
        Integer count = baseMapper.selectCount(wrapper3);
        if(count>0){
            return Result.error().message("该学生已注册");
        }


        //先得到学院和专业的编码，入职时间，生成工号
        String departmentName = sysStudent.getDepartmentName();
        String professionalName = sysStudent.getProfessionalName();
        String classCode = sysStudent.getClassCode();
        if(StringUtils.isEmpty(departmentName) || StringUtils.isEmpty(professionalName) || StringUtils.isEmpty(classCode)){
            return Result.error().message("请选择学院|专业|班级！");
        }
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_name",departmentName);
        SysDepartment sysDepartment = departmentMapper.selectOne(wrapper);
        String departmentCode = sysDepartment.getDepartmentCode();

        QueryWrapper<SysProfessional> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("professional_name",professionalName);
        SysProfessional sysProfessional = professionalMapper.selectOne(wrapper1);
        String professionalCode = sysProfessional.getProfessionalCode();

        String subIdNum = sysStudent.getHiredate().toString() + departmentCode + professionalCode + sysStudent.getClassCode();

        QueryWrapper<SysStudent> wrapper2 = new QueryWrapper<>();
        wrapper2.likeRight("stu_number",subIdNum);
        List<SysStudent> sysStudents = baseMapper.selectList(wrapper2);
//        System.out.println("-------------"+sysTeachers);
        long l;
        if(sysStudents.isEmpty()){ //所添加老师同专业及入职时间无同事那么拼接4个0
            subIdNum =  subIdNum + "0000";
            l = Long.parseLong(subIdNum);//拼接后工号
            sysStudent.setStuNumber(l);
        }else{
            Optional<Long> max = sysStudents.stream().map(SysStudent::getStuNumber).max(Long::compare);
            l = max.get() + 1;
            sysStudent.setStuNumber(l);
        }

        Long stuNumber = sysStudent.getStuNumber();
        if(stuNumber != l){
            sysStudent.setStuNumber(l);
        }
        int i = baseMapper.updateById(sysStudent);
        if (i > 0) {
            return Result.ok().message("修改成功！");
        } else {
            return Result.error().message("修改失败！");
        }
    }
}
